package com.example.piece.ui.activity


/**
 * 个人信息页面
 */
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.example.base.aop.click.RepeatClick
import com.example.base.aop.login.LoginCallback
import com.example.base.broadcast.Event
import com.example.base.ext.dip2px
import com.example.base.ext.getViewModel
import com.example.base.ext.navigationTo
import com.example.base.ext.setCornersPlaceHolderImageUrl
import com.example.base.ui.activity.BaseActivity
import com.example.base.vm.onError
import com.example.piece.App
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivityInfoBinding
import com.example.piece.entity.UserInfoTargetEntity
import com.example.piece.ui.adapter.ViewPagerAdapter
import com.example.piece.ui.fragment.*
import com.example.piece.vm.UserViewModel
import com.gyf.immersionbar.ktx.immersionBar
import com.gyf.immersionbar.ktx.statusBarHeight
import com.kongzue.dialogx.dialogs.BottomDialog
import com.kongzue.dialogx.interfaces.OnBindView

@Route(path = RoutePath.MAIN_INFO_ACTIVITY)
class InfoActivity : BaseActivity<ActivityInfoBinding>() {

    /**  个人中心选中类型   type=0 帖子 type=1 评论 type=2 赞过  type=3 收藏 **/
    @JvmField
    @Autowired(name = Key.DataKey.INFO_TYPE)
    var INFO_TYPE: String = ""

    override fun isRegisterEventBus(): Boolean = true
    lateinit var userInfo: UserInfoTargetEntity

    @JvmField
    @Autowired(name = Key.DataKey.TARGET_USER_ID)
    var TARGET_USER_ID: String = ""
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    override fun getContentRes(): Int = R.layout.activity_info
    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        immersionBar {
            fitsSystemWindows(false)
            statusBarDarkFont(true)
            //  statusBarAlpha(0.1f)
        }
        mBinding.statusBarHeight = statusBarHeight
        mBinding.activity = this
        val layoutParams = mBinding.rlFlow.layoutParams as CoordinatorLayout.LayoutParams
        layoutParams.height = (dip2px(50f) + statusBarAlpha).toInt()
        mBinding.rlFlow.layoutParams = layoutParams


        val layoutParamsrlMove = mBinding.rlMove.layoutParams as LinearLayout.LayoutParams
        layoutParamsrlMove.height = (dip2px(50f) + statusBarAlpha).toInt()
        mBinding.rlMove.layoutParams = layoutParamsrlMove

        val fragments = mutableListOf<Fragment>()
        if (TARGET_USER_ID == App.userLoginCodeEntity.user.userId) {
            val titles = listOf("作品 0", "喜欢 0", "评论 0", "收藏 0")
            fragments.add(ZuoPinFragment.newInstance(TARGET_USER_ID, 0))
            fragments.add(ZuoPinFragment.newInstance(TARGET_USER_ID, 1))
            fragments.add(PingLunFragment.newInstance(TARGET_USER_ID))
            fragments.add(ShouCangFragment.newInstance(TARGET_USER_ID, 0))
            mBinding.vp.adapter = ViewPagerAdapter(supportFragmentManager, fragments, titles)

            mBinding.tvBjzl.visibility = View.VISIBLE
            mBinding.tvSx.visibility = View.GONE
            mBinding.tvGuzu.visibility = View.GONE

        } else {
            val titles = listOf("作品 0", "喜欢 0")
            fragments.add(ZuoPinFragment.newInstance(TARGET_USER_ID, 0))
            fragments.add(ZuoPinFragment.newInstance(TARGET_USER_ID, 1))
            mBinding.vp.adapter = ViewPagerAdapter(supportFragmentManager, fragments, titles)

            mBinding.tvBjzl.visibility = View.GONE
            mBinding.tvSx.visibility = View.VISIBLE
            mBinding.tvGuzu.visibility = View.VISIBLE

        }

        mBinding.tab.setupWithViewPager(mBinding.vp)

        when (INFO_TYPE) {
            "0" -> {
                //mBinding.vp.setCurrentItem(0, false)
            }
            "1" -> {
                mBinding.vp.setCurrentItem(1, false)
            }

            "2" -> {
                mBinding.vp.setCurrentItem(2, false)
            }

            "3" -> {
                mBinding.vp.setCurrentItem(3, false)
            }

        }
        mBinding.vp.offscreenPageLimit = 5

        getUserInfo()
    }

    private fun getUserInfo() {
        userViewModel.userInfoTarget(this, TARGET_USER_ID) {
            if (it.code == 200) {
                userInfo = it.data
                mBinding.userInfoTargetEntity = it.data
                mBinding.imgHead.setCornersPlaceHolderImageUrl(
                    it.data.avatar,
                    90,
                    R.drawable.header_img
                )
                mBinding.imgBg.setCornersPlaceHolderImageUrl(
                    it.data.avatar,
                    1,
                    R.drawable.header_img
                )


                if (TARGET_USER_ID == App.userLoginCodeEntity.user.userId) {
                    mBinding.tab.getTabAt(0)?.text = "作品 ${it.data.jokesNum}"

                    mBinding.tab.getTabAt(1)?.text = "喜欢 ${it.data.jokeLikeNum}"

                    mBinding.tab.getTabAt(2)?.text = "评论 ${it.data.commentNum}"
                    mBinding.tab.getTabAt(3)?.text = "收藏 ${it.data.collectNum}"

                } else {
                    mBinding.tab.getTabAt(0)?.text = "作品 ${it.data.jokesNum}"

                    mBinding.tab.getTabAt(1)?.text = "喜欢 ${it.data.jokeLikeNum}"


                }

            }

        } onError {

        }
    }

    @RepeatClick
    fun onBackPress(view: View) {
        finish()

    }

    @RepeatClick
    fun onJuBao(view: View) {
        BottomDialog.build()
            .setCustomView(object : OnBindView<BottomDialog>(R.layout.dialog_jubao) {
                override fun onBind(dialog: BottomDialog?, v: View?) {
                    v?.findViewById<TextView>(R.id.tv_jubao)?.setOnClickListener { //举报

                        navigationTo(RoutePath.MAIN_JUBAO_ACTIVITY)
                        dialog?.dismiss()
                    }
                    v?.findViewById<TextView>(R.id.tv_cancel)?.setOnClickListener { //取消
                        dialog?.dismiss()
                    }
                }

            })
            .show()
    }

    @RepeatClick
    fun gotoInfo(view: View) {

        navigationTo(RoutePath.MAIN_USER_INFO_ACTIVITY)
    }

    override fun receiveEvent(event: Event<Any>?) {
        super.receiveEvent(event)
        val num = 0

        when (event?.code) {
            Key.EventKey.EVENT_USERINFO_ZP -> {//作品
                event?.data as Long
                mBinding.tab.getTabAt(0)?.text = "作品 ${userInfo.jokesNum.toLong().plus(num)}"

            }
            Key.EventKey.EVENT_USERINFO_LIKE -> {//喜欢
                event?.data as Long
                mBinding.tab.getTabAt(1)?.text = "喜欢 ${userInfo.jokeLikeNum.toLong().plus(num)}"

            }
            Key.EventKey.EVENT_USERINFO_PL -> {//评论
                event?.data as Long
                mBinding.tab.getTabAt(2)?.text = "评论 ${userInfo.commentNum.toLong().plus(num)}"

            }
            Key.EventKey.EVENT_USERINFO_SC -> {//收藏
                event?.data as Long
                mBinding.tab.getTabAt(3)?.text = "收藏${userInfo.collectNum.toLong().plus(num)}"

            }

        }

    }

    //粉丝和关注页面
    @LoginCallback
    @RepeatClick
    fun gotoFansAndGz(typeInfo: String) {
       /* if (!App.USER_TOKEN.isNullOrEmpty()) {*/
            navigationTo(RoutePath.MAIN_FUN_GZ_ACTIVITY, mutableMapOf(
                Key.DataKey.H5_TITLE to App.userLoginCodeEntity.user.nickname,
                Key.DataKey.FANS_GZ_TYPE to typeInfo.toInt()))
       // }


    }

    override fun onResume() {
        super.onResume()
        getUserInfo()

    }


}